package seq_diverse.main;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.ApplicationFrame;
import java.io.*;

import java.util.Arrays;

public class SeqDiverseMain {
    
    public static void main(String args[]) {
        if (args == null)
            args = new String[0];
        System.out.println("args=" + Arrays.asList(args));

	if(args.length < 2){
	    System.err.println("Usage:");
	    System.err.println("");
	    System.err.println("\tjava -jar SeqDiverse.jar <file type> <filename> [k]");
	    System.err.println("");
	    System.err.println("\twhere");
	    System.err.println("\tfile type: can be fq (for fastq) or sq (for sequence);");
	    System.err.println("\tfilename: name of file with sequences");
	    System.err.println("\tk (optional): length of k-mers (default: k=6)");
	    System.err.println("");
	    
	    return;
	}

	BaseParse fq = null;
	if(args[0].equals("fq")){
	    try {
		fq = new FASTQParse(args[1]);
	    }catch(IOException e){
		System.err.println("Error: Could not open " + args[1]);
		return;
	    }
	}else{
	    try {
		fq = new SimpleParse(args[1]);
	    }catch(IOException e){
		System.err.println("Error: Could not open " + args[1]);
		return;
	    }
	}
	String line;
	int k = 6;
	if(args.length > 2){
	    k = Integer.parseInt(args[2]);
	}
	if(k < 1){
	    System.err.println("Error: k must be a positive integer.");
	    return;
	}

	if((line = fq.getSequence()) == null) return;
	DirectMap dm = new DirectMap(line.length(), k);
	dm.insert(line);
	
	while((line = fq.getSequence()) != null){
	    dm.insert(line);
	}

	XYSeries series0 = dm.getMaximum();
	XYSeries series1 = dm.getDiversity();
	XYSeries series2 = dm.getRichness();
	XYSeries series3 = dm.getEvenness();

	final XYSeriesCollection data1 = new XYSeriesCollection();
	final XYSeriesCollection data2 = new XYSeriesCollection();
        data1.addSeries(series2);
	data1.addSeries(series3);
        data2.addSeries(series1);
        data2.addSeries(series0);

	final LineChart show = new LineChart(args[2] + "-mer Sequence Diversity", data1, data2);
	show.pack();
	RefineryUtilities.centerFrameOnScreen(show);
        show.setVisible(true);
    }
    
}
